package dm.ast.model.gen;

//根据模板AST生成GROUP子句

import dm.ast.DmASTNode;
import dm.ast.DmGroupby;
import dm.ast.model.gen.factory.BaseModelFactory;
import dm.ast.model.gen.factory.ComplexityModel;
import dm.ast.model.gen.factory.GroupModelFactory;
import test.utility.Randomly;

import java.util.ArrayList;
import java.util.List;

public class GroupGen extends ASTGenBase {
    public static boolean cubeFlag = false;
    public static boolean rollupFlag = false;
    public static boolean groupingsetsFlag = false;

    @Override
    public DmASTNode gen(int astLevel, int wid_t, int dep_t) {
        DmGroupby groupby = new DmGroupby();
        groupby.setTargetWidt(wid_t);
        groupby.setTargetDept(dep_t);
        ComplexityModel model = GroupModelFactory.groupModels(wid_t, dep_t);
        groupby.setItemSize(model.count);
        return groupby;
    }

    public static void GroupingSets(DmGroupby dmGroupby){
        List<Integer> tmpSize = new ArrayList<>();
        int count = dmGroupby.getItemSize();
        if(count == 0){
            tmpSize = null;
        }else {
            int itemSize1 = count;
            while (itemSize1 > 0){
                int integer = Randomly.getInteger(1,itemSize1);
                tmpSize.add(integer);
                itemSize1 -= integer;
            }
        }
        dmGroupby.setGroupingSets(tmpSize);
    }

    //随机给dmgroup中的CubeOrrollup赋值
    public static void flag(DmGroupby groupby, BaseModelFactory baseModelFactory) {
        cubeFlag = baseModelFactory.testFlags[BaseModelFactory.FLAG_GROUPBYCUBE];
        rollupFlag = baseModelFactory.testFlags[BaseModelFactory.FLAG_GROUPBYROLLUP];
        groupingsetsFlag = baseModelFactory.testFlags[BaseModelFactory.FLAG_GROUPBYGROUPINGSETS];

        ArrayList<Integer> tmpStatus = new ArrayList<>();
        tmpStatus.add(0);
        if (cubeFlag) {
            tmpStatus.add(1);
        }

        if (rollupFlag) {
            tmpStatus.add(2);
        }
        if(groupingsetsFlag){
            tmpStatus.add(3);
        }
        groupby.setCubeOrrollupOrGroupingsets(Randomly.fromList(tmpStatus));
    }
}
